package mspbots.cw.client.core.data.subdata;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.cw.client.core.AbstractToolsUtil;
import mspbots.cw.client.core.data.BaseSyncService;
import mspbots.cw.client.core.client.ClientProperties;
import mspbots.cw.common.QueryParams;
import mspbots.cw.common.RabbitKeys;
import mspbots.cw.common.ResultMsg;
import mspbots.cw.common.ResultSync;
import mspbots.cw.common.subdata.SubDataType;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * mspbots.data.cw.core.invoices.OrdersSubServiceImpl
 *
 * @author <a href="https://github.com/vnobo">Alex bob</a>
 * @date Created by 2020/9/8
 */
@Log4j2
@Service
@RequiredArgsConstructor
public class ScheduleStatusesServiceImpl extends AbstractToolsUtil implements BaseSyncService {

    private Mono<Void> syncScheduleStatuses(ClientProperties properties) {
        ResultSync resultSync = ResultSync.withDefault("SCHEDULE-STATUSES-SYNC");
        return this.getScheduleStatuses(properties)
                .contextWrite(ctx -> ctx.put("resultSync", resultSync))
                .doOnNext(resultNode -> this.syncResult(resultSync.getRequestId(), properties.getTenantId(),
                        SubDataType.SCHEDULE_STATUSES, resultNode))
                .then();
    }

    private Flux<JsonNode> getScheduleStatuses(ClientProperties properties) {
        return this.cwWebClient.fetch("/schedule/statuses", QueryParams.withDefault())
                .contextWrite(ctx -> ctx.put("properties", properties));
    }

    private Mono<Void> syncScheduleType(ClientProperties properties) {
        ResultSync resultSync = ResultSync.withDefault("SCHEDULE-TYPE-SYNC");
        return this.getScheduleType(properties)
                .contextWrite(ctx -> ctx.put("resultSync", resultSync))
                .doOnNext(resultNode -> this.syncResult(resultSync.getRequestId(), properties.getTenantId(),
                        SubDataType.SCHEDULE_TYPE, resultNode))
                .then();
    }

    private Flux<JsonNode> getScheduleType(ClientProperties properties) {
        return this.cwWebClient.fetch("/schedule/types", QueryParams.withDefault())
                .contextWrite(ctx -> ctx.put("properties", properties));
    }

    private Mono<Void> syncScheduleCalendars(ClientProperties properties) {
        ResultSync resultSync = ResultSync.withDefault("SCHEDULE-CALENDARS-SYNC");
        return this.getScheduleCalendars(properties)
                .contextWrite(ctx -> ctx.put("resultSync", resultSync))
                .doOnNext(resultNode -> this.syncResult(resultSync.getRequestId(), properties.getTenantId(),
                        SubDataType.SCHEDULE_CALENDARS, resultNode))
                .then();
    }

    private Flux<JsonNode> getScheduleCalendars(ClientProperties properties) {
        return this.cwWebClient.fetch("/schedule/calendars", QueryParams.withDefault())
                .contextWrite(ctx -> ctx.put("properties", properties));
    }

    @Override
    public Mono<Void> syncSlow(ClientProperties properties) {
        return Flux.concatDelayError(this.syncScheduleStatuses(properties),
                this.syncScheduleType(properties),
                this.syncScheduleCalendars(properties)).then();
    }

    private void syncResult(UUID uuid, Long tenantId, SubDataType dataType, JsonNode resultNode) {
        this.sendEntity(RabbitKeys.SUB_DATA_SYNC_QUEUE_KEY, ResultMsg.builder()
                .requestId(uuid).id(resultNode.get("id").longValue())
                .action(dataType.name()).entity(resultNode).tenantId(tenantId)
                .syncDate(LocalDateTime.now()).build());
    }

}
